using System;
using System.Windows.Forms;

using Elev.Core;

namespace Elev.UI
{
    public partial class GradeCommentForm : Form
    {
        Grade grade;

        public Grade Grade
        {
            get { return grade; }
        }

        public GradeCommentForm(Grade Grade)
        {
            InitializeComponent();
            
            grade = Grade;
            if (grade.Note == "")
                Text = "Ajouter un commentaire";
            else
                Text = "Editer un commentaire";

            // Fill in the form
            commentTextBox.Text = grade.Note;            

            okButton.Click += delegate
            {
                Ok();
            };

            cancelButton.Click += delegate
            {
                Cancel();
            };

            commentTextBox.Select();
        }

        void Ok()
        {
            grade.Note = commentTextBox.Text;

            try
            {
                grade.Save();
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        void Form_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Return:
                    if (!commentTextBox.Focused) Ok();
                    break;
                case Keys.Escape:
                    Cancel();
                    break;
            }
        }
    }
}